<?php
/**
 * Observación: NO PUEDE CAMBIARSE EL ORDEN DE FILTROS DEL BUSCADOR SIN TOMAR EN CUENTA SU ORDEN EN ESTE MODELO  
 * [TODO] hacer innecesaria la Observación
 *
 */


class TransaccionesCompraEliminaModel extends Zwei_Db_Table
{
	protected $_name = "transacciones_compra";
    protected $_primary = array("id_trans", "fecha_tran");
	protected $_name_elimina = "transacciones_elimina";
	
	protected $_is_filtered = true;
	protected $_form;
	protected $_searchFields = array();
	protected $_search = array();
	protected $_adapter = "dn";
	
    public function init()
    {
    	ini_set("memory_limit", "200M");
    	$this->initQueryParams();
    }
	
	public function select()
	{
		$select = new Zend_Db_Table_Select($this);
		$select->from(
		    $this->_name,
            array(
                'msisdn'=>'msisdn',           
                'id_promo',
                'modulo',
                'estado',
                'operacion',
                'costo',
                'fecha_tran',
                'origen' => new Zend_Db_Expr("IF(STRCMP(origen,''),origen,'-')")
            )		
		);
		
		if (!empty($this->_query_params['msisdn'])) 
            $select->where($this->getAdapter()->quoteInto("msisdn = ?", $this->_query_params['msisdn']));
        
        if (!empty($this->_query_params['fecha_tran'])) {
            $select->where($this->getAdapter()->quoteInto("fecha_tran >= ?", $this->_query_params['fecha_tran'][0]));
            $select->where($this->getAdapter()->quoteInto("fecha_tran <= ?", $this->_query_params['fecha_tran'][1]));
        }     
		
		$select_elimina = new Zend_Db_Table_Select($this);
		$select_elimina->setIntegrityCheck(false);
		$select_elimina->from(
            $this->_name_elimina,
            array(
                'msisdn'=>'msisdn',              
                'id_promo',
                'modulo',
                'estado',
                'operacion',
                'costo',
                'fecha_tran',
                'origen' => new Zend_Db_Expr("'-'")            
            )
        );

        if (!empty($this->_query_params['msisdn'])) 
            $select_elimina->where($this->getAdapter()->quoteInto("msisdn = ?", $this->_query_params['msisdn']));
        
        if (!empty($this->_query_params['fecha_tran'])) {
            $select_elimina->where($this->getAdapter()->quoteInto("fecha_tran >= ?", $this->_query_params['fecha_tran'][0]));
            $select_elimina->where($this->getAdapter()->quoteInto("fecha_tran <= ?", $this->_query_params['fecha_tran'][1]));
        }   
	
		$select_all = new Zend_Db_Table_Select($this);
		$select_all->union( array( $select , $select_elimina) );
		
		return $select_all;
	}

    public function __destruct() {
        ini_set("memory_limit", "128M");
    }	
	
/* Para consulta historica 
select_all:(SELECT id_promo, modulo, estado, operacion, costo, fecha_tran, IF(STRCMP(origen,''),origen,'-') 
origen FROM transacciones_compra WHERE msisdn='56983420073' AND DATE(fecha_tran) >= '2011-12-01' AND DATE(fecha_tran) <= '2012-04-01') 
UNION (SELECT id_promo, modulo, estado, operacion, costo, fecha_tran, '-' FROM transacciones_elimina WHERE msisdn='56983420073' 
AND DATE(fecha_tran) >= '2011-12-01' AND DATE(fecha_tran) <= '2012-04-01') ORDER BY fecha_tran desc
*/
}
